package com.yixun.service.impl;

import com.alibaba.fastjson.JSON;
import com.yixun.entity.Dict;
import com.yixun.entity.DictCategory;
import com.yixun.entity.YxUser;
import com.yixun.mapper.DictCategoryMapper;
import com.yixun.service.DictCategoryService;
import com.yixun.service.LogSetService;
import com.yixun.util.FormatUtil;
import com.yixun.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictCategoryServiceImpl implements DictCategoryService {

    @Autowired
    private DictCategoryMapper dictCategoryMapper;

    @Autowired
    private LogSetService logSetService;

    @Override
    public List<DictCategory> getDictCategoryList() {
        return dictCategoryMapper.getDictCategoryList();
    }

    @Override
    @Transactional
    public int insertDictCate(YxUser sessionUser, DictCategory dictCategory) {
        int row = 0;
        dictCategory.setId(new FormatUtil().getUUID());
        row = dictCategoryMapper.insertDictCate(dictCategory);
        if (row == 1 ){
            logSetService.insertSettingLogs(LogUtil.ADD, "数据字典分类", JSON.toJSONString(dictCategory), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    public DictCategory getDictCategory(String id) {
        return dictCategoryMapper.getDictCategory(id);
    }

    @Override
    @Transactional
    public int updateDictCate(YxUser sessionUser, DictCategory dictCategory) {
        int row = 0;
        row = dictCategoryMapper.updateDictCate(dictCategory);
        if (row == 1 ){
            logSetService.insertSettingLogs(LogUtil.UPDATE, "数据字典分类", JSON.toJSONString(dictCategory), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    @Transactional
    public int delDictCate(YxUser sessionUser, Map<String, String> map) {
        int row = 0;
        row = dictCategoryMapper.delDictCate(map);
        if (row > 0) {
            logSetService.insertSettingLogs(LogUtil.DELETE, "数据字典分类", JSON.toJSONString(map), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    @Transactional
    public int sortDictCate(YxUser sessionUser, Map<String, String> map) {
        int row = 0;
        String[] ids = map.get("id").split(",");
        Map<String,String> m = new HashMap<>();
        for (int i = 0; i < ids.length; i++) {
            m.put("id", ids[i]);
            m.put("sort_id", (i+1) + "");
            dictCategoryMapper.sortDictCate(m);
            row++;
        }
        if (row > 0) {
            logSetService.insertSettingLogs(LogUtil.SAVE_SORT, "数据字典分类", JSON.toJSONString(map), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    public List<Dict> getDictList(String dict_cat_id) {
        return dictCategoryMapper.getDictList(dict_cat_id);
    }

    @Override
    @Transactional
    public int insertDict(YxUser sessionUser, Dict dict) {
        int row = 0;
        dict.setId(new FormatUtil().getUUID());
        row = dictCategoryMapper.insertDict(dict);
        if (row == 1 ){
            logSetService.insertSettingLogs(LogUtil.ADD, "数据字典", JSON.toJSONString(dict), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    public Dict getDictBean(String id) {
        return dictCategoryMapper.getDictBean(id);
    }

    @Override
    @Transactional
    public int updateDict(YxUser sessionUser, Dict dict) {
        int row = 0;
        row = dictCategoryMapper.updateDict(dict);
        if (row == 1 ){
            logSetService.insertSettingLogs(LogUtil.UPDATE, "数据字典", JSON.toJSONString(dict), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    @Transactional
    public int delDict(YxUser sessionUser, Map<String, String> map) {
        int row = 0;
        row = dictCategoryMapper.delDict(map);
        if (row > 0){
            logSetService.insertSettingLogs(LogUtil.DELETE, "数据字典", JSON.toJSONString(map), sessionUser);
            return row;
        }
        return row;
    }

    @Override
    @Transactional
    public int sortDict(YxUser sessionUser, Map<String, String> map) {
        int row = 0;
        String[] ids = map.get("id").split(",");
        Map<String,String> m = new HashMap<>();
        for (int i = 0; i < ids.length; i++) {
            m.put("id", ids[i]);
            m.put("sort_id", (i+1) + "");
            dictCategoryMapper.sortDict(m);
            row++;
        }
        if (row > 0) {
            logSetService.insertSettingLogs(LogUtil.SAVE_SORT, "数据字典", JSON.toJSONString(map), sessionUser);
            return row;
        }
        return row;
    }
}
